/**
 * Project Name:ecsmts_mina
 * File Name:MinaServer.java
 * Package Name:com.aliyun.server
 * Date:2013-7-18下午4:11:24
 * Copyright (c) 2013, 13940488705@163.com All Rights Reserved.
 *
 */

package com.aliyun.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.filter.executor.OrderedThreadPoolExecutor;
import org.apache.mina.filter.keepalive.KeepAliveFilter;
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory;
import org.apache.mina.filter.keepalive.KeepAliveRequestTimeoutHandler;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.SocketSessionConfig;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.server.heartbeat.HeartBeatServerHandler;
import com.aliyun.server.heartbeat.KeepAliveMessageFactoryImpl;
import com.aliyun.server.heartbeat.KeepAliveRequestTimeoutHandlerImpl;

/**
 * ClassName: MinaServer <br/>
 * Function: the main function. <br/>
 * Reason: . <br/>
 * date: 2013-7-18 下午4:19:05 <br/>
 * 
 * @author zhangfeng
 * @version
 * @since JDK 1.6
 */
public class MinaServer {

	private static final Logger LOG = LoggerFactory.getLogger(MinaServer.class);

	private static final int PORT = 12000;
	/** 30秒后超时 */
	private static final int IDELTIMEOUT = 30;
	/** 15秒发送一次心跳包 */
	private static final int HEARTBEATRATE = 15;

	private static SocketAcceptor acceptor;
	private static ExecutorService filterExecutor = new OrderedThreadPoolExecutor();
	private static IoHandler handler = new HeartBeatServerHandler();

	public static void main(String[] args) throws IOException {

		acceptor = new NioSocketAcceptor(Runtime.getRuntime()
				.availableProcessors());
		acceptor.setReuseAddress(true);
		acceptor.getSessionConfig().setReadBufferSize(1024);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,
				IDELTIMEOUT);
		// Decrease the default receiver buffer size
		((SocketSessionConfig) acceptor.getSessionConfig())
				.setReceiveBufferSize(1024);

		acceptor.getFilterChain().addLast("threadPool",
				new ExecutorFilter(filterExecutor));
		acceptor.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new TextLineCodecFactory()));
		
		KeepAliveMessageFactory heartBeatFactory = new KeepAliveMessageFactoryImpl();
		KeepAliveRequestTimeoutHandler heartBeatHandler = new KeepAliveRequestTimeoutHandlerImpl();
		KeepAliveFilter heartBeat = new KeepAliveFilter(heartBeatFactory,
				IdleStatus.BOTH_IDLE, heartBeatHandler);
		/** 是否回发 */
		heartBeat.setForwardEvent(true);
		/** 发送频率 */
		heartBeat.setRequestInterval(HEARTBEATRATE);
		acceptor.getFilterChain().addLast("heartbeat", heartBeat);

		/** *********************** */

		acceptor.setHandler(handler);
		acceptor.bind(new InetSocketAddress(PORT));

		LOG.info("服务器开启，监听端口：" + PORT);

	}

}
